﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WcfService.Util;
using WcfService.DTO;
using System.Data;
using System.Data.OleDb;

namespace WcfService.DAO
{
    public class RssDAO:DataProvider
    {
        public RssDAO() {
            connect();
        }
        public RssDTO[] getRssUser(string ma)
        {
            string query = "SELECT * FROM RSS WHERE NguoiDung='"+ma+"' AND TinhTrang=1";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            RssDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new RssDTO[n];
            for (i = 0; i < n; i++)
            {
                object rss = GetDataFromDataRow(dt, i);
                arr[i] = (RssDTO)rss;
            }
            return arr;
        }
         public RssDTO[] getAll()
        {
            string query = "SELECT * FROM RSS";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            RssDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new RssDTO[n];
            for (i = 0; i < n; i++)
            {
                object rss = GetDataFromDataRow(dt, i);
                arr[i] = (RssDTO)rss;
            }
            return arr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            RssDTO rss = new RssDTO();
            rss.MaRSS = int.Parse(dt.Rows[i]["MaRSS"].ToString());
            rss.NguoiDung = dt.Rows[i]["NguoiDung"].ToString();
            rss.Link = dt.Rows[i]["Link"].ToString();
            rss.TinhTrang = int.Parse(dt.Rows[i]["TinhTrang"].ToString());

            return (object)rss;
        }

        public void insert(string nguoiDung, string link, int tinhTrang)
        {
            string insertCommand = "INSERT INTO TinMuaBan VALUES('" +
                                   nguoiDung + "','" +
                                   link + "'," +
                                   tinhTrang + ")";

            executeNonQuery(insertCommand);
            disconnect();
        }

        public void delete(int maRss)
        {
            string deleteCommand = "UPDATE TinMuaBan SET TinhTrang = 0 WHERE MaRSS = " + maRss;
            executeNonQuery(deleteCommand);
            disconnect();
        }

        public void update(int ma, string nguoiDung, string link, int tinhTrang)
        {
            string updateCommand = "UPDATE TinMuaBan SET NguoiDung = '" + nguoiDung + "',"
                 + "Link = '" + link + "',"
                + "TinhTrang = " + tinhTrang
                + "WHERE MaTinMuaBan = " + ma;
            executeNonQuery(updateCommand);
            disconnect();
        }
    }
}